package com.scuec.groupfive.service.impl;

import com.scuec.groupfive.entity.UserInfo;
import com.scuec.groupfive.mapper.UserproMapper;
import com.scuec.groupfive.service.UserproService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserproServiceImpl implements UserproService {

    @Autowired
    private UserproMapper userproMapper;

    @Override
    public UserInfo getUserInfoById(Integer id) {
        UserInfo userInfo = userproMapper.getUserInfoById(id);
        return userInfo;
    }

    @Override
    public UserInfo getUserByEmail(String email) {
        if (StringUtils.isEmpty(email)) {
            throw new IllegalArgumentException("邮箱不能为空");
        }
        return userproMapper.getUserByEmail(email);
    }

    @Override
    public Integer updateUserInfo(UserInfo userInfo) {
        // 可以在这里添加业务逻辑验证
        if (userInfo == null || userInfo.getUserId() == null) {
            throw new IllegalArgumentException("用户信息或用户ID不能为空");
        }

        Integer result = userproMapper.updateUserInfo(userInfo);
        return result;
    }

    @Override
    public Integer updateUserProfile(UserInfo userInfo) {
        // 验证用户信息
        if (userInfo == null || userInfo.getUserId() == null) {
            throw new IllegalArgumentException("用户信息或用户ID不能为空");
        }

        Integer result = userproMapper.updateUserProfile(userInfo);
        return result;
    }

    @Override
    public Integer updateUserPassword(UserInfo userInfo) {
        // 验证密码信息
        if (userInfo == null || userInfo.getUserId() == null || userInfo.getPassword() == null) {
            throw new IllegalArgumentException("用户ID或密码不能为空");
        }

        // 这里可以添加密码加密逻辑
        // userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));

        Integer result = userproMapper.updateUserPassword(userInfo);
        return result;
    }

    @Override
    public Integer updateLastLoginTime(Integer userId) {
        if (userId == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }

        Integer result = userproMapper.updateLastLoginTime(userId);
        return result;
    }

    @Override
    public Integer deleteUserById(Integer userId) {
        if (userId == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }

        // 实际项目中应添加关联数据删除逻辑（如用户帖子、评论等）
        // 示例：commentService.deleteCommentsByUserId(userId);
        // 示例：postService.deletePostsByUserId(userId);

        return userproMapper.deleteUserById(userId);
    }
}